//
//  CustomSpannerView.m
//  laowu
//
//  Created by 尹桥印 on 15/4/30.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "CustomSpannerView.h"

#define kItemBtnHeight 30
#define kBaseIndex 56

@interface CustomSpannerView()
@property (nonatomic,assign)CGFloat showWidth;
@property (nonatomic,weak)UIView *bearingView;
@property (nonatomic, weak) UIScrollView *scrollView;
@end
@implementation CustomSpannerView

-(instancetype)initWithStringArray:(NSArray *)array bearingView:(UIView *)bearingView{
    self = [super init];
    if (self) {
        [self setFrame:[UIScreen mainScreen].bounds];
        self.bearingView = bearingView;
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[bearingView convertRect: bearingView.bounds toView:window];
        self.showWidth = rect.size.width;
        
        UIButton *coverBtn = [[UIButton alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [self addSubview:coverBtn];
        [coverBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(rect.origin.x, CGRectGetMaxY(rect), self.showWidth, (array.count>6?6:array.count)*kItemBtnHeight)];
        [scrollView setShowsHorizontalScrollIndicator:YES];
        [scrollView setBackgroundColor:[UIColor whiteColor]];
        
        for (int i=0; i<array.count; i++) {
            UIButton *button = [self itemBtnWithTitle:array[i] tag:i+kBaseIndex];
            [button setY:i*kItemBtnHeight];
            [scrollView addSubview:button];
            [scrollView setContentSize:CGSizeMake(self.showWidth, CGRectGetMaxY(button.frame))];
        }
        [scrollView setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
    }
    return self;
}

-(instancetype)initWithStringArray:(NSArray *)array showPoint:(CGPoint)showPoint width:(CGFloat)width
{
    self = [super init];
    if (self) {
        [self setFrame:[UIScreen mainScreen].bounds];
        self.showWidth = width;
        UIButton *coverBtn = [[UIButton alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [self addSubview:coverBtn];
        [coverBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(showPoint.x, showPoint.y, width, (array.count>6?6:array.count)*kItemBtnHeight)];
        [scrollView setBackgroundColor:[UIColor whiteColor]];
        
        for (int i=0; i<array.count; i++) {
            UIButton *button = [self itemBtnWithTitle:array[i] tag:i+kBaseIndex];
            [button setY:i*kItemBtnHeight];
            [scrollView addSubview:button];
            [scrollView setContentSize:CGSizeMake(width, CGRectGetMaxY(button.frame))];
        }
        
        [scrollView setCornerRadius:0 borderWidth:1 borderColor:HEXColor(@"#dfdfdf")];
        [self addSubview:scrollView];
    }
    return self;
}

-(UIButton *)itemBtnWithTitle:(NSString *)title tag:(NSInteger)tag{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.showWidth, kItemBtnHeight)];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [btn.titleLabel setFont:SYS_FONT(14)];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTag:tag];
    return btn;
}

-(void)itemBtnClick:(UIButton *)btn{
    if (_delegate) {
        [_delegate customSpannerItemClick:btn.titleLabel.text index:btn.tag-kBaseIndex bearingView:self.bearingView];
    }
    [self hide];
}

-(void)setIsTop:(BOOL)isTop{
    _isTop = isTop;
    if (_isTop) {
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[self.bearingView convertRect: self.bearingView.bounds toView:window];
        [self.scrollView setY:rect.origin.y-self.scrollView.height];
    }
}
-(void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

-(void)hide{
    [self removeFromSuperview];
}

@end
